//Player Module

//Adds the other functions in the player module
//To a given Entity object
//Goal is to use this function to simulate inheritance
//For the player object in a more intelligent manner
function addPlayerProperties(){
	player.updatePlayer = updatePlayer;
	player.drawPlayer = drawPlayer;
	player.handleOrbitChange = handleOrbitChange;
	player.handleDirectionChange = handleDirectionChange;
	player.handleSpriteRotation = handleSpriteRotation;
	//Governs whether or not the player is currently changing orbit.
	//0 indicates no orbital change, while -1 indicates a decrease
	//in orbital height and 1 indicates an increase in orbital height
	player.isChangingOrbit = 0;
	player.playerUp = playerUp;
	player.playerDown = playerDown;
	player.playerLeft = playerLeft;
	player.playerRight = playerRight;
	player.playerSpace = playerSpace;
	//Governs whether or not the player is currently changing direction
	//0 indicates no direction change, while 1 indicates the player is
	//currently changing direction.
	player.isChangingDirection = false;
	//Represent the offset of the player from its current orbit
	//as a result of orbital transitions
	player.xOffset = 0;
	player.yOffset = 0;
	//Used for sprite animation
	player.angleOffset = 0;
}

//functions to be added to the Player object

//Updates the player's position, and also translates
//The canvas to follow the player
updatePlayer = function(){
	var oldx = this.pos.x;
	var oldy = this.pos.y;
	
	if(this.isChangingDirection){
		this.handleSpriteRotation();
		this.handleDirectionChange();
	}else{
		this.angle = (this.angle + this.angVel)%(Math.PI*2);
		if(this.angle < 0){this.angle = Math.PI*2 - this.angle;}
		this.pos = Orbit_getPos(this.orbit, this.angle);
		this.handleOrbitChange();
	}
	ctxOffsetX += (this.pos.x - oldx); ctxOffsetX2 += (this.pos.x - oldx);
	ctxOffsetY += (this.pos.y - oldy); ctxOffsetY2 += (this.pos.y - oldy);
	ctx.translate(oldx - this.pos.x, oldy - this.pos.y); //Note: origin must be translated in the opposite direction
};

drawPlayer = function(){
	this.drawEntity(this.angleOffset);
	var dist = Math.sqrt( Math.abs(Math.pow(this.pos.x - boss.pos.x, 2)+ Math.pow(this.pos.y - boss.pos.y, 2)) );
	var vector = {x:(this.pos.x - boss.pos.x)/dist, y:(this.pos.y - boss.pos.y)/dist};
	ctx.beginPath();
	ctx.moveTo(this.pos.x, this.pos.y);
	ctx.strokeStyle = "red";
	ctx.lineWidth = 1;
	ctx.lineTo(this.pos.x - vector.x*45, this.pos.y - vector.y*45);
	ctx.stroke();
};

//Checks for and handles orbital transitions
handleOrbitChange = function(){
	if(this.isChangingDirection){return;}
	if(this.isChangingOrbit == 1){
		var planetVector = Orbit_getPlanetVector(this.pos.x, this.pos.y);
		this.xOffset += (planetVector.x)*orbitalChangeSpeed;
		this.yOffset += (planetVector.y)*orbitalChangeSpeed;
		this.pos.x += this.xOffset; this.pos.y += this.yOffset;
		this.angVel = Orbit_getAngVelFree(this.pos.x, this.pos.y, this.direction);
		for(var i = 0; i < orbitPositions.length; i++){
			if(this.orbit == i) continue;
			if(Math.abs(orbitPositions[i] - Orbit_getPlanetDistance(this.pos.x, this.pos.y)) < ORBIT_SNAP_THRESHOLD){
				this.isChangingOrbit = 0;
				this.orbit = i;
				this.pos = Orbit_getPos(this.orbit, this.angle);
				this.angVel = Orbit_getAngVelClosed(this.orbit, this.direction);
				this.xOffset = 0; this.yOffset = 0;
				break;
			}
		}
	}else if(this.isChangingOrbit == -1){
		var planetVector = Orbit_getPlanetVector(this.pos.x, this.pos.y);
		this.xOffset -= (planetVector.x)*orbitalChangeSpeed;
		this.yOffset -= (planetVector.y)*orbitalChangeSpeed;
		this.pos.x += this.xOffset; this.pos.y += this.yOffset;
		this.angVel = Orbit_getAngVelFree(this.pos.x, this.pos.y, this.direction);
		for(var i = 0; i < orbitPositions.length; i++){
			if(this.orbit == i) continue;
			if(Math.abs(orbitPositions[i] - Orbit_getPlanetDistance(this.pos.x, this.pos.y)) < ORBIT_SNAP_THRESHOLD){
				this.isChangingOrbit = 0;
				this.orbit = i;
				this.pos = Orbit_getPos(this.orbit, this.angle);
				this.angVel = Orbit_getAngVelClosed(this.orbit, this.direction);
				this.xOffset = 0; this.yOffset = 0;
				break;
			}
		}
	}
};


//Checks for and handles directional changes
handleDirectionChange = function(){
	//if(this.isChangingOrbit != 0 || this.isChangingDirection == false){return;}
	this.angVel += Orbit_getAngVelClosed(this.orbit-2, this.direction*(-1))*(orbitalChangeSpeed)/(ORBIT_DISTANCE);
	this.angle += this.angVel;
	
	var planetVector = Orbit_getPlanetVector(this.pos.x, this.pos.y);
	this.xOffset -= (planetVector.x)*orbitalChangeSpeed;
	this.yOffset -= (planetVector.y)*orbitalChangeSpeed;
	this.pos = Orbit_getPos(this.orbit, this.angle);
	this.pos.x += this.xOffset; this.pos.y += this.yOffset;
	for(var i = 0; i < orbitPositions.length; i++){
		if(i == this.orbit || i == this.orbit-1){continue;}
		if(Math.abs(orbitPositions[i] - Orbit_getPlanetDistance(this.pos.x, this.pos.y)) < ORBIT_SNAP_THRESHOLD){
				this.isChangingDirection = 0;
				this.direction *= -1;
				this.orbit = i;
				this.pos = Orbit_getPos(this.orbit, this.angle);
				this.angVel = Orbit_getAngVelClosed(this.orbit, this.direction);
				this.xOffset = 0; this.yOffset = 0;
				break;
			}
	}
};


handleSpriteRotation = function(){
	if (this.direction === 1) {
		this.angleOffset += Math.PI/frametime/1.25;
		if (this.angleOffset > Math.PI) this.angleOffset = Math.PI;
	} else {
		this.angleOffset -= Math.PI/frametime/1.25;
		if (this.angleOffset < 0) this.angleOffset = 0;
	}
};


//Handles player controls, with dynamic control adjustments
//When in the upper half of an orbit, upkey causes the player to move
//farther from the center and downkey causes the player to closer. These
//controls reverse when the player is in the lower half of an orbit. Likewise,
//in the right half of an orbit, rightkey moves the player farther and leftkey
//moves them closer, with these controls reversed in the left half of an orbit.
playerDown = function(){
	if(this.isChangingOrbit == 0 && this.isChangingDirection != true){
		if(this.angle < Math.PI/2 || this.angle > Math.PI*(3/2)){
			if(this.orbit > 0){this.isChangingOrbit = -1;}
		}else{
			if(this.orbit < ORBIT_COUNT-1){this.isChangingOrbit = 1;}
		}
	}
};

playerUp = function(){
	if(this.isChangingOrbit == 0 && this.isChangingDirection != true){
		if(this.angle < Math.PI/2 || this.angle > Math.PI*(3/2)){
			if(this.orbit < ORBIT_COUNT-1){this.isChangingOrbit = 1;}
		}else{
			if(this.orbit > 0){this.isChangingOrbit = -1;}
		}
	}
};

playerRight = function(){
	if(this.isChangingOrbit == 0 && this.isChangingDirection != true){
		if(this.angle < Math.PI){
			if(this.orbit < ORBIT_COUNT-1){this.isChangingOrbit = 1;}
		}else{
			if(this.orbit > 0){this.isChangingOrbit = -1;}
		}
	}
};

playerLeft = function(){
	if(this.isChangingOrbit == 0 && this.isChangingDirection != true){
		if(this.angle < Math.PI){
			if(this.orbit > 0){this.isChangingOrbit = -1;}
		}else{
			if(this.orbit < ORBIT_COUNT-1){this.isChangingOrbit = 1;}
		}
	}
};

playerSpace = function(){
	if((this.isChangingOrbit == 0 && this.isChangingDirection != true) && this.orbit > 1){
		this.isChangingDirection = true;
	}
};
